package com.alibaba.travel.article.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: where
 * @Date: 2023/03/07/8:59
 * @Description:
 */
@Configuration
@ConfigurationProperties(prefix = "thread.pool")
@Data
public class ThreadPoolConfig {
    private Integer coreSize;
    private Integer maxSize;
    private Long keepAliveTime;
    private TimeUnit unit = TimeUnit.SECONDS;
    private BlockingQueue<Runnable> workQueue = new LinkedBlockingDeque<>(10000);

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        // 还有一个参数是拒绝策略
        return new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime, unit, workQueue,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
